﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UltimateSkillManager : MonoBehaviour
{
    Text moneyValue;//元宝数
    public static float aniSpeed = 1;//动画的速度
    public Animator[] ani;//动画
    public GameObject congratulationsPanel;//恭喜获得的panel
    public SMainItem sMainItem;//技能的数据总库
    public GameObject[] getThings;//获得的碎片的展示位
    public Text grade1;//展示的技能的等级
    public Text content1;//展示的技能的属性
    public Text grade2;//展示的技能的等级
    public Text content2;//展示的技能的属性
    public Text needfragments;//强化需要的碎片数量
    public GameObject replacePanel;//更换

    public GameObject yindao13;
    void Start()
    {
        yindao13.transform.GetChild(2).gameObject.SetActive(false);
        yindao13.transform.GetChild(1).gameObject.SetActive(false);
        if (VariableDocument.GetInstance.yindaoid == 12)
        {
            yindao13.transform.GetChild(2).gameObject.SetActive(true);
            yindao13.transform.GetChild(1).gameObject.SetActive(true);
        }
        Random.InitState((int)System.DateTime.Now.Ticks);
        moneyValue = GameObject.Find("moneyValue").GetComponent<Text>();
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        SkillShow();
    }

    void Update()
    {
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
    }
    public void DrawOne()
    {
        if (VariableDocument.GetInstance.yindaoid == 12)
        {
            yindao13.transform.GetChild(2).gameObject.SetActive(false);
            yindao13.transform.GetChild(1).gameObject.SetActive(false);
            VariableDocument.GetInstance.yindaoid = 15;
        }
        if (VariableDocument.GetInstance.money >= 188)
        {
            VariableDocument.GetInstance.money -= 188;
            aniSpeed = 24;
            for (int a = 0; a < 8; a++)
            {
                ani[a].SetBool("to", true);
                ani[a].SetBool("back", false);
            }
            Invoke("CongratulationsPanel", 3f);
        }
        
    }
    void CongratulationsPanel()
    {
        congratulationsPanel.SetActive(true);
        //获得两种技能碎片，每种得到碎片1-5片
        int skill1 = Random.Range(0, 8);
        int skill2 = Random.Range(0, 8);
        int num1 = Random.Range(1, 4);
        int num2 = Random.Range(1, 4);
        while (skill1 == skill2)
        {
            skill2 = Random.Range(0, 8);
        }
        getThings[0].SetActive(true);
        getThings[1].SetActive(true);
        getThings[0].transform.GetChild(1).GetComponent<Image>().sprite = sMainItem.itemList[skill1].sitemImage;
        getThings[1].transform.GetChild(1).GetComponent<Image>().sprite = sMainItem.itemList[skill2].sitemImage;
        getThings[0].transform.GetChild(2).GetComponent<Text>().text = "X" + num1;
        getThings[1].transform.GetChild(2).GetComponent<Text>().text = "X" + num2;
        sMainItem.itemList[skill1].fragmentsNum += num1;
        sMainItem.itemList[skill2].fragmentsNum += num2;
        for (int a = 0; a < 8; a++)
        {
            ani[a].SetBool("back", true);
            ani[a].SetBool("to", false);
        }
        aniSpeed = 1;
    }

    //抽10次
    public void Drawten()
    {
        if (VariableDocument.GetInstance.money >= 1688)
        {
            VariableDocument.GetInstance.money -= 1688;
            aniSpeed = 24;
            for (int a = 0; a < 8; a++)
            {
                ani[a].SetBool("to", true);
                ani[a].SetBool("back", false);
            }
            Invoke("CongratulationsPanel2", 3f);
        }

    }

    void CongratulationsPanel2()
    {
        congratulationsPanel.SetActive(true);
        //获得两种技能碎片，每种得到碎片1-5片
        int[] skill = { 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] num = { 0, 0, 0, 0, 0, 0, 0, 0 };
        //十次获得的技能碎片都有什么
        for (int b = 0; b < 10; b++)
        {
            int c = Random.Range(0, 8);//为技能id
            skill[c] += 1;
            
        }
        //每种技能获得的碎片数
        for (int b = 0; b < 8; b++)
        {
            int c = Random.Range(2, 8);//数量
            num[b] = skill[b] * c;
        }
        //展示
        int d = 0;//展示框的序号
        for(int b= 0; b < 8; b++)
        {
            if(skill[b] != 0)
            {
                getThings[d].SetActive(true);
                getThings[d].transform.GetChild(0).GetComponent<Image>().sprite = sMainItem.itemList[b].sitemImage;
                getThings[d].transform.GetChild(1).GetComponent<Text>().text = "X" + num[b];
                sMainItem.itemList[skill[b]].fragmentsNum += num[b];
                d++;
            }
        }
        for (int a = 0; a < 8; a++)
        {
            ani[a].SetBool("back", true);
            ani[a].SetBool("to", false);
        }
        aniSpeed = 1;
    }
    //获得之后点击关闭
    public void CloseGet()
    {
        for (int b = 0; b < 8; b++)
        {
            getThings[b].SetActive(false);
        }
        congratulationsPanel.SetActive(false);
    }
    //技能区的展示
    void SkillShow()
    {
        grade1.text = "+" + sMainItem.itemList[VariableDocument.GetInstance.skill_id].sitemGrade;
        content1.text = sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[0] + "\n" + sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[1] + "秒";
        grade2.text = "+" + (sMainItem.itemList[VariableDocument.GetInstance.skill_id].sitemGrade + 1);
        content2.text = (sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[0] + 300) + "\n" + (sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[1] - 5) + "秒";
        needfragments.text = ((sMainItem.itemList[VariableDocument.GetInstance.skill_id].sitemGrade + 1) * 50).ToString();
    }
    //点击强化
    public void StrengthenSkill()
    {
        int frag = sMainItem.itemList[VariableDocument.GetInstance.skill_id].fragmentsNum;
        int grad = (sMainItem.itemList[VariableDocument.GetInstance.skill_id].sitemGrade + 1) * 50;
        if (frag >= grad)
        {
            sMainItem.itemList[VariableDocument.GetInstance.skill_id].fragmentsNum -= grad;
            sMainItem.itemList[VariableDocument.GetInstance.skill_id].sitemGrade += 1;
            sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[0] += 300;
            sMainItem.itemList[VariableDocument.GetInstance.skill_id].attribute[1] -= 5;
            SkillShow();
        }
    }
    //点击更换
    public void ReplaceSkill()
    {
        replacePanel.SetActive(true);
    }
    //点击空白处关闭
    public void ReplaceClose()
    {
        replacePanel.SetActive(false);
    }
}
